import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';

import { PersonInfo } from '../person-info';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-info-update-form',
  templateUrl: './info-update-form.component.html',
  styleUrls: ['./info-update-form.component.less']
})
export class InfoUpdateFormComponent implements OnInit {

  constructor( private router: Router ) { }

  private personInfo = new PersonInfo('', '', '', '', '');

  @ViewChild('InfoForm') public InfoForm: NgForm;

  initial() {
    // 从本地存储取出personInfo
    const info = JSON.parse(localStorage.getItem('info'));
    // 绑定展示
    this.personInfo = info;
  }

  onClickBtnCancel() {
    if (JSON.stringify(this.InfoForm.value) !== localStorage.getItem('info')) {
      const status = confirm('信息已作修改，确定取消？');
      if (status === true) {
        this.initial();
      } else {
          return;
      }
    }
  }

  onSubmit() {
    localStorage.setItem('info', JSON.stringify(this.personInfo));
    alert('修改成功！');
  }

  ngOnInit() {
    this.initial();
  }

}
